<%
  description ||= 'image'
  thumbnail ||= 'medium'
  toggle_image_display ||= false
  conditions ||= nil
  randomiser = rand(Time.now.yesterday.to_i * Time.now.to_i)
  current_image_link_href = refinery.insert_admin_images_path(:dialog => true, :callback => "image_picker_#{randomiser}_changed",
                                                    :width => 866, :height => 510, :conditions => conditions)
  current_image_title = t('.change', :what =>  description.titleize)
-%>
<div id='image_picker_container_<%= randomiser %>'>
  <%= link_to t('.show'), '', :class => "current_image_toggler" if toggle_image_display %>
  <%= f.hidden_field field, :class => "current_image_id" -%>

  <div class='current_image_container' style="margin-top: 10px<%= ";display: none" if toggle_image_display %>">
    <a class='current_image_link dialog' href="<%= current_image_link_href %>" style='border: 0px' title='<%= current_image_title  %>' name='<%= current_image_title %>'>
      <% if image.nil? -%>
        <img class="current_picked_image" src="" alt="" style='display: none' />
      <% else -%>
        <%= image_fu image, Refinery::Images.user_image_sizes[thumbnail.to_sym],
                     :class => "brown_border current_picked_image" %>
      <% end -%>
      <span class='no_picked_image_selected nothing_selected'<%= " style='display: none;'".html_safe if image.present? %>>
        <%= t('.none_selected') %>
      </span>
    </a>
    <br/>
    <%= link_to t('.remove_current'), "#",
                :class => "remove_picked_image",
                :style => "#{"display:none;" if image.nil?}" %>
  </div>
</div>

<% content_for :javascripts do -%>
  <script>
    var image_picker_<%= randomiser %> = {};
    $(document).ready(function(e) {
      $.extend(true, image_picker_<%= randomiser %>, image_picker)
      image_picker_<%= randomiser %> = image_picker_<%= randomiser %>.init({
        thumbnail: '<%= thumbnail.html_safe %>'
        , image_toggler: '<%= toggle_image_display ? ".current_image_toggler" : "null" %>'
        , description: '<%= description %>'
        , field: '.current_image_id'
        , image_display: '.current_picked_image'
        , no_image_message: '.no_picked_image_selected'
        , remove_image_button: '.remove_picked_image'
        , picker_container: '#image_picker_container_<%= (lambda {randomiser}).call %>'
        , image_container: '.current_image_container'
        , image_link: '.current_image_link'
      });
    });

    image_picker_<%= randomiser %>_changed = function(args){
      $.proxy(image_picker_<%= randomiser %>.changed, {
        container: '#image_picker_container_<%= randomiser %>'
        , picker: image_picker_<%= randomiser %>
        , image: args
      })();
    };
  </script>
<% end %>
